import { apiPost, apiGet } from './client';

export interface Message {
  id: string;
  role: 'USER' | 'ASSISTANT';
  content: string;
  metadata?: any;
  createdAt: string;
}

export interface CreateMessageRequest {
  content: string;
  sessionId: string;
  metadata?: any;
}

export interface CreateMessageResponse {
  userMessage: Message;
  aiResponse: Message;
}

export interface MessageListResponse {
  items: Message[];
  total: number;
  page: number;
  pageSize: number;
}

export interface MessageListQuery {
  page?: number;
  pageSize?: number;
  order?: 'asc' | 'desc';
}

export const messageApi = {
  // Send message
  async createMessage(data: CreateMessageRequest): Promise<CreateMessageResponse> {
    return await apiPost('/messages', data);
  },

  // Get session message list
  async getSessionMessages(
    sessionId: string,
    query: MessageListQuery = {}
  ): Promise<MessageListResponse> {
    const params = new URLSearchParams();
    if (query.page) params.append('page', query.page.toString());
    if (query.pageSize) params.append('pageSize', query.pageSize.toString());
    if (query.order) params.append('order', query.order);

    return await apiGet(`/messages/session/${sessionId}?${params}`);
  },

  // Get session latest messages
  async getLatestMessages(sessionId: string, limit?: number): Promise<Message[]> {
    const params = new URLSearchParams();
    if (limit) params.append('limit', limit.toString());

    return await apiGet(`/messages/session/${sessionId}/latest?${params}`);
  },
};
